<template>
  <el-container  class="home-container">
    <el-header>
      <div>
        <img src="../assets/images/header.jpg" alt="">
        <span>小明的博客后台管理系统</span>
      </div>
    <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
      <!-- 折叠按钮 -->
      <div class="collapse" @click="toggleCollapse">|||</div>
      <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
        <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
          <template slot="title">
            <i :class="iconsObj[item.id]"></i>
            <span>{{item.authName}}</span>
          </template>
          <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="savePath('/' + subItem.path)">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{subItem.authName}}</span>
            </template>
          </el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data () {
    return {
      menulist: [],
      iconsObj: {
        100: 'el-icon-s-order',
        200: 'el-icon-s-comment',
        300: 'el-icon-folder-opened',
        400: 'el-icon-mobile',
        500: 'el-icon-s-promotion',
        600: 'el-icon-user-solid'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activepath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('home/menu')
      if (res.meta.status !== 200) return this.$messgae.error('res.meta.msg')
      this.menulist = res.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    savePath (activePath) {
      window.sessionStorage.setItem('activepath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
    display: flex;
    background-color: #373d41;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;
    div {
      display: flex;
      align-items: center;
      img {
       height: 50px;
      }
      span {
        margin-left: 10px;
        color: #fff;
        font-size: 18px;
      }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
    i {
      margin-right: 10px;
    }
  }
}
.collapse {
  background-color: #4a5064;
  line-height: 24px;
  text-align: center;
  font-size: 10px;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
